package com.zt.douyamusic.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zt.douyamusic.R;

/**
 * Author: ZT on 2017/2/21.
 */
public class DescActivity extends BaseActivity{
    private WebView webView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_desc);
        initView();
    }
    private void initView() {
        webView = (WebView) findViewById(R.id.desc_webview);
        String url = getIntent().getStringExtra("url");
        webView.loadUrl(url);
        WebSettings setting = webView.getSettings();
        setting.setBuiltInZoomControls(true);//显示缩放按钮
        setting.setUseWideViewPort(true);//支持缩放
        setting.setJavaScriptEnabled(true);//支持js功能(js实现动态网页，标签控制/请求服务器...)

        webView.setWebViewClient(new WebViewClient() {
            //开始加载网页
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }
            //网页加载结束
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
            //所有的连接跳转都会回调此方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);//连接跳转时，强制在当前webview加载
                return true;
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
            if(webView.canGoBack()){
                webView.goBack();//返回上一张网页
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(webView!=null){
            webView.destroy();//解决微博页面退出后，视频还在播放的bug
        }
    }
}
